from django.db import models
from clbum.models import *


# 基类
class BaseModels(models.Model):
    create_time = models.DateTimeField("创建时间", auto_now_add=True)

    is_delete = models.BooleanField("是否删除", default=False)

    class Meta:

        abstract = True


# 学校表
class SchoolModel(BaseModels):
    name = models.CharField("学校名称", max_length=20)
    about = models.CharField("学校简介", max_length=256)

    class Meta:
        db_table = "school_tb"
        verbose_name_plural = "学校表"

    def __str__(self):
        return self.name


# 班级
class ClasssModel(BaseModels):
    name = models.CharField("班级名称", max_length=10)
    school = models.ForeignKey(SchoolModel, verbose_name="所属学校", on_delete=models.CASCADE)

    class Meta:
        db_table = "classs_tb"
        verbose_name_plural = "班级表"

    def __str__(self):
        return self.name


# 角色表
class RoleModel(BaseModels):
    name = models.CharField("角色", max_length=20)

    class Meta:
        db_table = "role_tb"
        verbose_name_plural = "角色表"

    def __str__(self):
        return self.name


# 用户表
class UserModel(BaseModels):
    account = models.CharField("账号", max_length=20)
    password = models.CharField("密码", max_length=256)
    classs = models.ForeignKey(ClasssModel, verbose_name="所属班级", on_delete=models.CASCADE)
    user = models.IntegerField(verbose_name='所属老师', null=True)
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name="所属角色")

    class Meta:
        db_table = "user_tb"
        verbose_name_plural = "用户表"

    def __str__(self):
        return self.account


# 个人信息
class UserInfoModel(BaseModels):
    SEX = (
        (0, "男"),
        (1, "女")
    )
    name = models.CharField("名称", max_length=20)
    img = models.CharField("头像", max_length=256)
    gender = models.SmallIntegerField(verbose_name="性别", choices=SEX, default=0)
    birthday = models.DateField("生日", max_length=20)
    mobile = models.CharField("手机号", max_length=11)
    user = models.ForeignKey(UserModel, verbose_name="所属用户", on_delete=models.CASCADE)

    class Meta:
        db_table = "user_info_tb"
        verbose_name_plural = "个人信息"

    def __str__(self):
        return self.name


# 菜单表
class MenuModel(BaseModels):
    name = models.CharField("菜单名称", max_length=100)
    path = models.CharField("菜单地址", max_length=100)
    # 关联自己
    menu = models.IntegerField(verbose_name='上级菜单', null=True)

    class Meta:
        db_table = "menu_tb"
        verbose_name_plural = "菜单表"

    def __str__(self):
        return self.name


# 角色菜单表
class RoleMenuModel(BaseModels):
    role = models.ForeignKey(RoleModel,on_delete=models.CASCADE, verbose_name='所属角色')
    menu = models.ForeignKey(MenuModel,on_delete=models.CASCADE, verbose_name='所属菜单')

    class Meta:
        db_table = 'role_menu_tb'
        verbose_name = '角色菜单表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.role.name} 拥有 {self.menu.name} 菜单'


# 权限表
class PermissionModel(BaseModels):
    name = models.CharField('权限名称', max_length=100)
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name="所属角色")

    class Meta:
        db_table = 'permission_tb'
        verbose_name_plural = '权限表'

    def __str__(self):
        return self.name


# 权限角色中间表
class PermissionRole(BaseModels):
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name="所属角色")
    permission = models.ForeignKey(PermissionModel, on_delete=models.CASCADE, verbose_name="所属权限")

    class Meta:
        db_table = "permission_role_tb"
        verbose_name_plural = "权限角色表"

    def __str__(self):
        return "%s角色的权限为%s"%(self.role.name, self.permission.name)


# 登陆记录
class UserLoginModel(BaseModels):
    og_time = models.DateTimeField("登陆时间", auto_now_add=True)
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="登陆用户id")

    class Meta:
        db_table = "login_tb"
        verbose_name_plural = "登录记录"

    def __str__(self):
        return "%s员工登陆了" % (self.user.account)


# 问候语
class GreetingsModel(BaseModels):
    name = models.CharField("问候语内容", max_length=256)
    start_time = models.IntegerField("开始时间")
    end_time = models.IntegerField("结束时间")

    class Meta:
        db_table = "greetings_tb"
        verbose_name_plural = "问候语表"

    def __str__(self):
        return self.name


# 节日问候语表
class FesGreetingsModel(BaseModels):

    TYPE = (
        (1, '阳历'),
        (2, '阴历')
    )

    content = models.CharField('问候内容', max_length=256)
    holiday_name = models.CharField('节日名称', max_length=10)
    holiday_date = models.CharField('节日日期', max_length=20)
    date_type = models.SmallIntegerField('日期类型', choices=TYPE, default=1)

    class Meta:
        db_table = 'fes_greetings_tb'
        verbose_name = '节日问候语表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.holiday_name} 在 {self.holiday_date}'


# 搜索记录表
class SearchModel(BaseModels):
    name = models.CharField("搜索记录", max_length=256)
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="所属用户")

    class Meta:
        db_table = "search_tb"
        verbose_name_plural = "搜索记录表"

    def __str__(self):
        return self.name


# 订单
class OrderModel(models.Model):
    ORDER_STATE = (
        (0, "未支付"),
        (1, "已支付")
    )
    user = models.ForeignKey(verbose_name="用户", to=UserModel, on_delete=models.CASCADE)
    pay_state = models.SmallIntegerField(verbose_name="订单状态", choices=ORDER_STATE, default=0)
    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    pay_time = models.DateTimeField(verbose_name="支付时间", null=True)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    total_amount = models.DecimalField(verbose_name="总金额", max_digits=7, decimal_places=2)
    total_count = models.IntegerField(verbose_name="总数量")
    order_id = models.CharField(verbose_name="订单编号", max_length=256)
    # 支付方式
    pay_method = models.CharField("支付方式", max_length=10, choices=(("1", "支付宝"), ("2", "银联")), default="1")

    class Meta:
        db_table = "order_tb"
        verbose_name = "订单表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "订单 %s "% (self.id)


# 消息
class MessageModel(BaseModels):
    name = models.CharField("消息内容", max_length=50)
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="消息发送人", related_name="消息发送人")
    users = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='消息接收人', related_name='消息接收人')

    class Meta:
        db_table = "message_tb"
        verbose_name_plural = "消息表"

    def __str__(self):
        return "%s对%s%s"%(self.user.account, self.users.account, self.name)


# 轮播图
class CarouselModel(models.Model):
    imgPath = models.CharField("轮播图地址", max_length=256)
    describes = models.CharField("描述信息", max_length=100)

    class Meta:
        db_table = "carousel_tb"
        verbose_name_plural = "轮播图"

    def __str__(self):
        return "轮播图%d"%self.id


# 分类
class ClassIfyModel(BaseModels):
    name = models.CharField("名称", max_length=20)
    classify = models.IntegerField(verbose_name='上级分类', null=True)

    class Meta:
        db_table = "classify_tb"
        verbose_name_plural = "分类表"

    def __str__(self):
        return self.name


# 标签表
class TagModel(BaseModels):
    name = models.CharField("名称", max_length=20)
    user = models.ForeignKey(UserModel, verbose_name="所属用户", on_delete=models.CASCADE)

    class Meta:
        db_table = "tag_tb"
        verbose_name_plural = "标签表"

    def __str__(self):
        return "%s用户的标签为%s"%(self.user.account, self.name)


# 学习内容表
class ContentsModel(BaseModels):
    title = models.CharField("标题", max_length=100)
    content = models.TextField("内容")
    intro = models.CharField("简介", max_length=100)
    is_open = models.BooleanField("是否公开", default=False)
    classify = models.ForeignKey(ClassIfyModel, on_delete=models.CASCADE, verbose_name='所属分类')
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="作者", null=True)

    class Meta:
        db_table = "contents_tb"
        verbose_name_plural = "学习内容表"

    def __str__(self):
        return self.title


# 标签内容表
class tag_content_tb(BaseModels):
    content = models.ForeignKey(ContentsModel, on_delete=models.CASCADE, verbose_name='所属内容')
    tag = models.ForeignKey(TagModel, on_delete=models.CASCADE, verbose_name='所属标签')

    class Meta:
        db_table = 'tag_content_tb'
        verbose_name = '标签内容表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.content.content} 包含 {self.tag.name}'


# 学习资源表
class WealthModel(BaseModels):
    name = models.CharField("名称", max_length=50)
    depict = models.CharField("描述", max_length=100)
    price = models.DecimalField("价格", max_digits=5, decimal_places=2)
    sales = models.IntegerField("销量")
    user = models.ForeignKey(UserModel, verbose_name="作者", on_delete=models.CASCADE)

    class Meta:
        db_table = "wealth_tb"
        verbose_name_plural = "学习资源表"

    def __str__(self):
        return self.name


# 学习内容和学习资源中间表
class ContentsWealthModel(BaseModels):
    contents = models.ForeignKey(ContentsModel, on_delete=models.CASCADE, verbose_name="内容")
    wealth = models.ForeignKey(WealthModel, on_delete=models.CASCADE, verbose_name="资料")

    class Meta:
        db_table = "contents_wealth_tb"
        verbose_name = "资源"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.wealth


# 学习任务表
class TaskModel(BaseModels):
    content = models.ForeignKey(ContentsModel, on_delete=models.CASCADE, verbose_name="所属内容")
    classs = models.ForeignKey(ClasssModel, on_delete=models.CASCADE, verbose_name="所属班级")

    class Meta:
        db_table = 'task_tb'
        verbose_name = '学习任务表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.classs.name} 有 {self.content.title}'


# 学习记录表
class RecordModel(BaseModels):

    ORDER_STATE = (
        (1, '未完成'),
        (2, '作弊完成'),
        (3, '已完成'),
        (4, '费力完成'),
    )

    task = models.ForeignKey(TaskModel, on_delete=models.CASCADE, verbose_name='所属任务')
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='完成人')
    is_status = models.SmallIntegerField('状态', choices=ORDER_STATE, default=1)
    tokinaga = models.CharField(verbose_name='时长', max_length=50, null=True, default=None)
    start_time = models.DateTimeField('开始时间', null=True, default=None)
    finish_time = models.DateTimeField('完成时间', null=True, default=None)

    class Meta:
        db_table = 'record_tb'
        verbose_name = '学习记录表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.account} 费时 {self.tokinaga} {self.is_status}'


# 收藏表
class CollectModel(BaseModels):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="所属用户")
    wealth = models.ForeignKey(WealthModel, on_delete=models.CASCADE, verbose_name="所属资源")

    class Meta:
        db_table = "collect_tb"
        verbose_name_plural = "收藏表"

    def __str__(self):
        return "%s收藏的资源为%s"% (self.user.account, self.wealth.name)


# 评论表
class CommentModel(BaseModels):
    content = models.CharField("评论内容", max_length=100)
    user = models.ForeignKey(UserModel, verbose_name="所属用户", on_delete=models.CASCADE)
    contents = models.ForeignKey(ContentsModel, verbose_name="学习内容", on_delete=models.CASCADE)

    class Meta:
        db_table = "comment_tb"
        verbose_name_plural = "评论表"

    def __str__(self):
        return "%s的评论内容为%s"%(self.user.account, self.content)


# 弹幕表
class BarrageModel(BaseModels):
    name = models.CharField("弹幕内容", max_length=50)
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="所属用户")
    contents = models.ForeignKey(ContentsModel, on_delete=models.CASCADE, verbose_name="所属内容")
    like = models.IntegerField("被点赞量")

    class Meta:
        db_table = "barrage_tb"
        verbose_name_plural = "弹幕表"

    def __str__(self):
        return self.name



